package com.study.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.study.dao.entity.MovieUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author sunxuejian
 * @date 2020/4/8
 */
@Service
public class MovieUserService {

    @Autowired
    RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "selectByIdFallback")
    public MovieUser selectById(int id) {
        //spring.application.name=sunxj-microservice-provider-user
        //vip=virtual ip
        System.out.println("hystrix thread: " + Thread.currentThread().getName());
        MovieUser movieUser = restTemplate.getForObject("http://sunxj-microservice-provider-user/simple/" + id, MovieUser.class);
        return movieUser;
    }

    @HystrixCommand(fallbackMethod = "selectByIdFallback")
    public MovieUser selectById2(int id) throws InterruptedException {
        //spring.application.name=sunxj-microservice-provider-user
        //vip=virtual ip
        Thread.sleep(500);
        System.out.println("hystrix thread: " + Thread.currentThread().getName());
        MovieUser movieUser = restTemplate.getForObject("http://sunxj-microservice-provider-user/simple/" + id, MovieUser.class);
        return movieUser;
    }

    public MovieUser selectByIdFallback(int id) {
        //spring.application.name=sunxj-microservice-provider-user
        //vip=virtual ip
        MovieUser movieUser = new MovieUser();
        movieUser.setId(0);
        return movieUser;
    }

}
